#!/usr/bin/env node
import { choicesPackageManages, filterPackageManager } from 'create-ts-next';
import yargs, { command } from 'yargs';
import { resourceAggregation } from './aggr';
import { ScfCreator } from './scf';
import { TcbCreator } from './tcb';
import { filterCloudBaseRegion } from './utils';

yargs
  .command(
    'tcb <name>',
    'Create a Tencent Cloud Base (TCB) project.',
    _ =>
      _.positional('name', {
        describe: 'The project name',
        type: 'string',
        demandOption: true,
      }).options({
        dir: {
          alias: 'd',
          type: 'string',
          describe: 'Working directory',
        },
        install: {
          alias: 'i',
          type: 'boolean',
          default: true,
          describe: 'Install the dependencies',
        },
        'package-manager': {
          alias: 'p',
          type: 'string',
          default: 'pnpm',
          choices: choicesPackageManages(),
          describe: 'Choice node.js package manager, recommend pnpm',
        },
        envId: {
          alias: 'e',
          type: 'string',
          describe: 'TCB envId',
        },
        region: {
          alias: 'r',
          type: 'string',
          default: undefined,
          describe: 'TCB region',
        },
      }),
    async ({ name, install, packageManager, dir, envId, region }) => {
      await new TcbCreator({
        dir,
        name,
        install,
        envId,
        region: filterCloudBaseRegion(region),
        packageManager: filterPackageManager(packageManager),
      }).startUp();
    },
  )
  .command(
    'scf <name>',
    'Create a Serverless Cloud Function (SCF) subproject into a TCB project.',
    _ =>
      _.positional('name', {
        describe: 'The subproject name',
        type: 'string',
        demandOption: true,
      }).options({
        dir: {
          alias: 'd',
          type: 'string',
          describe: 'Working directory',
        },
        install: {
          alias: 'i',
          type: 'boolean',
          default: true,
          describe: 'Install the dependencies',
        },
        'package-manager': {
          alias: 'p',
          type: 'string',
          default: 'pnpm',
          choices: choicesPackageManages(),
          describe: 'Choice node.js package manager, recommend pnpm',
        },
      }),
    async ({ name, install, packageManager, dir }) => {
      await new ScfCreator({ dir, name, install, packageManager: filterPackageManager(packageManager) }).startUp();
    },
  );
command(
  'aggr',
  'Aggregate SCF config to cloudbaserc.json',
  _ =>
    _.option({
      dir: {
        alias: 'd',
        type: 'string',
        describe: 'Working directory',
      },
    }),
  async ({ dir }) => {
    resourceAggregation(dir);
  },
)
  .showHelpOnFail(false, 'Add --help option to get more helps')
  .help().argv;
